package com.itheima.service.pmsService.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.PageBean;
import com.itheima.commons.R;
import com.itheima.domain.db.pms.PmsProduct;
import com.itheima.dto.pms.PmsProductDto;
import com.itheima.mapper.pmsMapper.PmsProductMapper;
import com.itheima.service.pmsService.PmsProductService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsProductServiceImpl implements PmsProductService {

    @Autowired
    PmsProductMapper pmsProductMapper;

    //查询商品
    @Override
    public PageBean<PmsProduct> getByIdProduct(Integer brandId, String keyword, Integer pageNum, Integer pageSize, Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {
        IPage<PmsProduct> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        //商品品牌编号
        queryWrapper.eq(brandId!=null,PmsProduct::getBrandId, brandId);
        //商品名称模糊关键字
        queryWrapper.like(keyword!=null,PmsProduct::getName, keyword);
        //商品分类编号
        queryWrapper.eq(productCategoryId!=null,PmsProduct::getProductCategoryId, productCategoryId);
        //商号货号
        queryWrapper.eq(productSn!=null,PmsProduct::getProductSn, productSn);
        //上架状态
        queryWrapper.eq(publishStatus!=null,PmsProduct::getPublishStatus, publishStatus);
        //审核状态
        queryWrapper.eq(verifyStatus!=null,PmsProduct::getVerifyStatus, verifyStatus);


        pmsProductMapper.selectPage(page, queryWrapper);

        PageBean<PmsProduct> pageBean = new PageBean<>(page);

        return pageBean;
    }

    @Override
    public void publishStatus(Integer publishStatus, Integer[] ids) {
        for (Integer id : ids) {
            PmsProduct pmsProduct = new PmsProduct();
            pmsProduct.setId(Convert.toLong(id));
            pmsProduct.setPublishStatus(publishStatus);
            pmsProductMapper.updateById(pmsProduct);
        }
    }

    @Override
    public void newStatus(Integer newStatus, Integer[] ids) {
        for (Integer id : ids) {
            PmsProduct pmsProduct = new PmsProduct();
            pmsProduct.setId(Convert.toLong(id));
            pmsProduct.setNewStatus(newStatus);
            pmsProductMapper.updateById(pmsProduct);
        }
    }

    @Override
    public void recommendStatus(Integer recommendStatus, Integer[] ids) {
        for (Integer id : ids) {
            PmsProduct pmsProduct = new PmsProduct();
            pmsProduct.setId(Convert.toLong(id));
            pmsProduct.setRecommandStatus(recommendStatus);
            pmsProductMapper.updateById(pmsProduct);
        }
    }

    @Override
    public void add(PmsProduct pmsProduct) {
        pmsProductMapper.insert(pmsProduct);
    }

    @Override
    public List<PmsProduct> getSimpleList(Integer keyword) {

        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        //商品品牌编号
        lqw.like( keyword != null,PmsProduct::getKeywords,keyword);

        List<PmsProduct> pmsProducts = pmsProductMapper.selectList(lqw);

        return pmsProducts;
    }

    @Override
    public void updateDeleteStatus(Integer deleteStatus, Integer[] ids) {
        for (Integer id : ids) {
            LambdaUpdateWrapper<PmsProduct> lqw = new LambdaUpdateWrapper<>();
            lqw.eq(PmsProduct::getId,id);
            lqw.set(PmsProduct::getDeleteStatus,deleteStatus);
            pmsProductMapper.update(null,lqw);
        }
    }

    @Override
    public PmsProduct getInfoByBrandId(Long id) {
        PmsProduct pmsProduct = pmsProductMapper.selectById(id);
        return pmsProduct;
    }

    @Override
    public void update(PmsProductDto productDto) {
        pmsProductMapper.updateById(productDto);
    }
}
